home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / fsusage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-03  |  5.8 KB  |  217 lines

  1. /* fsusage.c -- return space usage of mounted filesystems
  2.    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #include <sys/types.h>
  23. #include "fsusage.h"
  24.  
  25. int statfs ();
  26.  
  27. #if defined (STATFS_OSF1)    /* DEC Alpha running OSF/1 */
  28. #  include <sys/mount.h>
  29. #endif
  30.  
  31. #if defined(STAT_STATFS2_BSIZE) && !defined(_IBMR2) /* 4.3BSD, SunOS 4, HP-UX, AIX PS/2.  */
  32. #include <sys/vfs.h>
  33. #endif
  34.  
  35. #ifdef STAT_STATFS2_FSIZE    /* 4.4BSD.  */
  36. #include <sys/mount.h>
  37. #endif
  38.  
  39. #ifdef STAT_STATFS2_FS_DATA    /* Ultrix.  */
  40. #include <sys/param.h>
  41. #include <sys/mount.h>
  42. #endif
  43.  
  44. #ifdef STAT_READ        /* SVR2.  */
  45. #include <sys/param.h>
  46. #include <sys/filsys.h>
  47. #include <fcntl.h>
  48. #endif
  49.  
  50. #if defined(STAT_STATFS4) || (defined(_AIX) && defined(_IBMR2)) /* SVR3, Dynix, Irix, AIX RS6000.  */
  51. #include <sys/statfs.h>
  52. #endif
  53.  
  54. #if defined(_AIX) && defined(_I386) /* AIX PS/2.  */
  55. #include <sys/stat.h>
  56. #include <sys/dustat.h>
  57. #endif
  58.  
  59. #ifdef STAT_STATVFS        /* SVR4.  */
  60. #include <sys/statvfs.h>
  61. int statvfs ();
  62. #endif
  63.  
  64. /* Return the number of TOSIZE-byte blocks used by
  65.    BLOCKS FROMSIZE-byte blocks, rounding up.  */
  66.  
  67. static long
  68. adjust_blocks (blocks, fromsize, tosize)
  69.      long blocks;
  70.      int fromsize, tosize;
  71. {
  72.   if (fromsize == tosize)    /* E.g., from 512 to 512.  */
  73.     return blocks;
  74.   else if (fromsize > tosize)    /* E.g., from 2048 to 512.  */
  75.     return blocks * (fromsize / tosize);
  76.   else                /* E.g., from 256 to 512.  */
  77.     return (blocks + 1) / (tosize / fromsize);
  78. }
  79.  
  80. /* Fill in the fields of FSP with information about space usage for
  81.    the filesystem on which PATH resides.
  82.    DISK is the device on which PATH is mounted, for space-getting
  83.    methods that need to know it.
  84.    Return 0 if successful, -1 if not. */
  85.  
  86. int
  87. get_fs_usage (path, disk, fsp)
  88.      char *path, *disk;
  89.      struct fs_usage *fsp;
  90. {
  91. #if defined (STATFS_OSF1)
  92.   struct statfs fsd;
  93.  
  94.   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
  95.     return (-1);
  96. #define convert_blocks(b) adjust_blocks ((b),fsd.f_fsize, 512)
  97. #endif /* STATFS_OSF1 */
  98.  
  99. #ifdef STAT_STATFS2_FS_DATA    /* Ultrix.  */
  100.   struct fs_data fsd;
  101.  
  102.   if (statfs (path, &fsd) != 1)
  103.     return -1;
  104. #define convert_blocks(b) adjust_blocks ((b), 1024, 512)
  105.   fsp->fsu_blocks = convert_blocks (fsd.fd_req.btot);
  106.   fsp->fsu_bfree = convert_blocks (fsd.fd_req.bfree);
  107.   fsp->fsu_bavail = convert_blocks (fsd.fd_req.bfreen);
  108.   fsp->fsu_files = fsd.fd_req.gtot;
  109.   fsp->fsu_ffree = fsd.fd_req.gfree;
  110. #endif
  111.  
  112. #ifdef STAT_READ        /* SVR2.  */
  113. #ifndef SUPERBOFF
  114. #define SUPERBOFF (SUPERB * 512)
  115. #endif
  116.   struct filsys fsd;
  117.   int fd;
  118.  
  119.   fd = open (disk, O_RDONLY);
  120.   if (fd < 0)
  121.     return -1;
  122.   lseek (fd, (long) SUPERBOFF, 0);
  123.   if (read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
  124.     {
  125.       close (fd);
  126.       return -1;
  127.     }
  128.   close (fd);
  129. #define convert_blocks(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
  130.   fsp->fsu_blocks = convert_blocks (fsd.s_fsize);
  131.   fsp->fsu_bfree = convert_blocks (fsd.s_tfree);
  132.   fsp->fsu_bavail = convert_blocks (fsd.s_tfree);
  133.   fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
  134.   fsp->fsu_ffree = fsd.s_tinode;
  135. #endif
  136.  
  137. #ifdef STAT_STATFS2_BSIZE    /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
  138.   struct statfs fsd;
  139.  
  140.   if (statfs (path, &fsd) < 0)
  141.     return -1;
  142. #define convert_blocks(b) adjust_blocks ((b), fsd.f_bsize, 512)
  143. #endif
  144.  
  145. #ifdef STAT_STATFS2_FSIZE    /* 4.4BSD.  */
  146.   struct statfs fsd;
  147.  
  148.   if (statfs (path, &fsd) < 0)
  149.     return -1;
  150. #define convert_blocks(b) adjust_blocks ((b), fsd.f_fsize, 512)
  151. #endif
  152.  
  153. #ifdef STAT_STATFS4        /* SVR3, Dynix, Irix.  */
  154.   struct statfs fsd;
  155.  
  156.   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
  157.     return -1;
  158.   /* Empirically, the block counts on most SVR3 and SVR3-derived
  159.      systems seem to always be in terms of 512-byte blocks,
  160.      no matter what value f_bsize has.  */
  161. #define convert_blocks(b) (b)
  162. #ifndef _SEQUENT_        /* _SEQUENT_ is DYNIX/ptx.  */
  163. #ifndef DOLPHIN            /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
  164. #define f_bavail f_bfree
  165. #endif
  166. #endif
  167. #endif
  168.  
  169. #ifdef STAT_STATVFS        /* SVR4.  */
  170.   struct statvfs fsd;
  171.  
  172.   if (statvfs (path, &fsd) < 0)
  173.     return -1;
  174.   /* f_frsize isn't guaranteed to be supported.  */
  175. #define convert_blocks(b) \
  176.   adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
  177. #endif
  178.  
  179. #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ) /* !Ultrix && !SVR2.  */
  180.   fsp->fsu_blocks = convert_blocks (fsd.f_blocks);
  181.   fsp->fsu_bfree = convert_blocks (fsd.f_bfree);
  182.   fsp->fsu_bavail = convert_blocks (fsd.f_bavail);
  183.   fsp->fsu_files = fsd.f_files;
  184.   fsp->fsu_ffree = fsd.f_ffree;
  185. #endif
  186.  
  187.   return 0;
  188. }
  189.  
  190. #if defined(_AIX) && defined(_I386)
  191. /* AIX PS/2 does not supply statfs.  */
  192.  
  193. int
  194. statfs (path, fsb)
  195.      char *path;
  196.      struct statfs *fsb;
  197. {
  198.   struct stat stats;
  199.   struct dustat fsd;
  200.  
  201.   if (stat (path, &stats))
  202.     return -1;
  203.   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  204.     return -1;
  205.   fsb->f_type   = 0;
  206.   fsb->f_bsize  = fsd.du_bsize;
  207.   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  208.   fsb->f_bfree  = fsd.du_tfree;
  209.   fsb->f_bavail = fsd.du_tfree;
  210.   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
  211.   fsb->f_ffree  = fsd.du_tinode;
  212.   fsb->f_fsid.val[0] = fsd.du_site;
  213.   fsb->f_fsid.val[1] = fsd.du_pckno;
  214.   return 0;
  215. }
  216. #endif /* _AIX && _I386 */
  217.